package com.lzd.pgyuploader

import com.lzd.pgyuploader.utils.ExecuteUtil
import com.lzd.pgyuploader.utils.HandleState
import com.lzd.pgyuploader.utils.TextUtil
import org.gradle.api.Plugin
import org.gradle.api.Project

class TaskAndUploader implements Plugin<Project> {
    static final def PGY_UPLOAD_AFTER = 'PGY_UPLOAD_AFTER'
    static final def PGY_UPLOAD_FILE = 'PGY_UPLOAD_FILE'
    static final def PGY_U_KEY = 'PGY_U_KEY'
    static final def PGY_API_KEY = 'PGY_API_KEY'

    @Override
    void apply(Project project) {
        def rootProject = project.rootProject
        String uploadAfter = rootProject.properties.get(PGY_UPLOAD_AFTER)
        if (!checkValue(uploadAfter, PGY_UPLOAD_AFTER)) {
            return
        }
        def uploader = project.tasks.create('PGYUploader')
        if (project.tasks.findByName('build') == null) {
            project.tasks.whenTaskAdded { task ->
                if (task.name == uploadAfter) {
                    initTask(uploader, uploadAfter, rootProject)
                }
            }
        } else {
            initTask(uploader, uploadAfter, rootProject)
        }
    }

    static void initTask(uploader, uploadAfter, project) {
        uploader.dependsOn(uploadAfter)
        uploader.doLast {
            uploadPGY(project)
        }
    }

    static void uploadPGY(project) {
        println TextUtil.getColorString('----- 开始上传到蒲公英 ------', TextUtil.Color.yellow)
        def uKey = project.properties.get(PGY_U_KEY)
        def apiKey = project.properties.get(PGY_API_KEY)
        String uploadFilePath = project.extensions.findByName(PGY_UPLOAD_FILE)
        boolean rst = true
        rst = rst && checkValue(uKey, PGY_U_KEY)
        rst = rst && checkValue(apiKey, PGY_API_KEY)
        rst = rst && checkValue(uploadFilePath, PGY_UPLOAD_FILE, ValueFrom.Extensions)
        if (!rst) {
            return
        }
        def uploadFile = new File(uploadFilePath)
        if (!uploadFile.exists()) {
            println TextUtil.getColorString("no such file!，蒲公英上传失败！ - ${uploadFile.getAbsolutePath()}", TextUtil.Color.red)
            return
        }
        def exe = "curl -F \"file=@${uploadFile.getAbsolutePath()}\" " +
                "-F 'uKey= ${uKey}' -F '_api_key= ${apiKey}' https://upload.pgyer.com/apiv1/app/upload"
        HandleState nowState = ExecuteUtil.execute(exe, "上传蒲公英成功", "上传蒲公英失败")
        if (!nowState.isRunning) {
            println TextUtil.getColorString(nowState.msg, TextUtil.Color.red)
        }
    }

    static boolean checkValue(value, property, from = ValueFrom.Properties) {
        if (value == null || value == '') {
            if (from == ValueFrom.Extensions) {
                println("""
${TextUtil.getColorString("""you haven't set ${property} in project!
you need set  ${property} like: 
extensions.add('${property}', 'value****') 
in project build.gradle""", TextUtil.Color.red)}
""")
            } else if (from == ValueFrom.Properties) {
                println("""
${TextUtil.getColorString("""you haven't add ${property} in project!
you need add ${property} like: 
${property} = value 
in project gradle.properties""", TextUtil.Color.red)}
""")
            }
            return false
        }
        return true
    }

    enum ValueFrom {
        Extensions,
        Properties,
    }
}
